package com.lgq.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.lgq.dao.CollegeDAO;
import com.lgq.model.College;
import com.lgq.util.Page;
@Component("collegeDAO")
public class CollegeDAOImpl implements CollegeDAO {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	@Override
	public Page<College> getAllCollege(int currentPage,int numPerPage) {
		Page<College> page=new Page<College>(currentPage,numPerPage);
		page.setTotalCount(Integer.parseInt((Long) getHibernateTemplate().find("select count(*) from College").get(0)+""));
		String hql="from College";
		page.setList(getListForPage(hql,(page.getCurrentPage()-1)*numPerPage, numPerPage));
		return page;
	}

	@Override
	public boolean addCollege(College college) {
		try
		{
			this.hibernateTemplate.save(college);
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
	@Override
	public boolean updateCollege(College college) {
		try
		{
			this.hibernateTemplate.merge(college);
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteCollege(int id) {
		try
		{			
			this.hibernateTemplate.delete(this.hibernateTemplate.load(College.class, id));
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public College findByName(String name) {
		List<College> colleges=this.hibernateTemplate.find("from College c where c.name='"+name+"'");
		if(colleges.size()>0)
		{
			return colleges.get(0);
		}
		else
		return null;
	}

	@Override
	public College findByVolId(String volid) {
		List<College> colleges=this.hibernateTemplate.find("from College c where c.volid='"+volid+"'");
		if(colleges.size()>0)
		{
			return colleges.get(0);
		}
		else
		return null;
	}

	@Override
	public List<College> findAllCollege() {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.loadAll(College.class);
	}

	@Override
	public College findCollegeById(int id) {
		// TODO Auto-generated method stub
		return (College) this.hibernateTemplate.get(College.class, id);
	}

	public List getListForPage(final String hql, final int offset,
			final int length) {

			List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
			Query query = session.createQuery(hql);
			query.setFirstResult(offset);
			query.setMaxResults(length);
			List list = query.list();
			return list;
			}
			});
			return list;
	}
}
